package eu.best.sercheng.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableModel;

import eu.best.sercheng.model.ResultQuery;
import eu.best.sercheng.model.dao.MainDao;
import eu.best.sercheng.model.dao.MainDaoImpl;


public class Gui extends JFrame {
	
	public class MyComparator implements Comparator {

		@Override
		public int compare(Object o1, Object o2) {
			// TODO Auto-generated method stub
			return 0;
		}
		
	}
	
	public class SearchButtonActionListener implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {
			
			List<ResultQuery> bigList = new ArrayList<ResultQuery>();
			
			 StringTokenizer st = new StringTokenizer(textToSearch.getText());
		     while (st.hasMoreTokens()) {
		         String s = st.nextToken();
		         List<ResultQuery> list =  dao.getResultsForKeyword(s);
					if(list == null) {
							list = new ArrayList<ResultQuery>();
							System.out.println("a");
					}
				bigList.addAll(list);
		     }
//		     Collections.sort(bigList, )
//			System.out.println(bigList.size());
			Object[][] t = convertListTo2DArray(bigList);

			TableModel model=new MyTableModel(t);
			table.setModel(model);
		}
}
	
	public class HtmlFrame extends JFrame {
		public HtmlFrame() {
			JEditorPane jep = new JEditorPane();  
		     jep.setSize(512, 342);  
		     jep.setEditable(false);     
		       
		     try {  
		       jep.setPage("http://www.java.com");  
		     } catch(MalformedURLException mex) {  
		       
		        jep.setContentType("text/html");  
		        jep.setText("<html><p>aa</p> </html>");  
		     } catch(IOException ex) { 
		    	 ex.printStackTrace();
		    	 }  
		      
		       JScrollPane scrollPane = new JScrollPane(jep);       
		       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
		       this.getContentPane().add(scrollPane);  
		       this.setSize(512,342);  
		       this.show();  
			add(scrollPane);
			setVisible(true);
		}
	}
	
	private JLabel searchLabel;
	private JTextField textToSearch;
	private JButton searchButton;
	private JButton showButton = new JButton("Show page");
	Object[] columnNames = {"Counter","Url","Priority"};
	Object[][] data;
	JTable table;
	MainDao dao = new MainDaoImpl();
	
	public Gui(){
		
		setLayout(new BorderLayout());
		
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		
		searchLabel= new JLabel("Search:");
		panel.add(searchLabel,BorderLayout.WEST);
		textToSearch=new JTextField();
		panel.add(textToSearch,BorderLayout.CENTER);
		searchButton=new JButton("Find");
		searchButton.addActionListener(new SearchButtonActionListener());
		panel.add(searchButton, BorderLayout.EAST);
		
		add(panel,BorderLayout.NORTH);
		
		Object[][] inputData = new Object[0][3];
		//Object[] tmp = {"1", "1", "1"};
		//inputData[0] = tmp;
		table = new JTable(new MyTableModel(inputData));
		add(new JScrollPane(table), BorderLayout.CENTER);
		
		//add(showButton,BorderLayout.SOUTH);
		
		showButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int i = table.getSelectedRow();
				MyTableModel tmp = (MyTableModel) table.getModel();
				Object[][] data = tmp.getData();
				String url = (String) data[i][1];
				new HtmlFrame();
			}
		});
		
		setSize(500,200);
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
	}
	
	public Object[][] convertListTo2DArray(List<ResultQuery> list) {
		Object[][] data = new Object[list.size()][];
		int i = 0;
		for(ResultQuery rq: list){
			Object[] tmp = { i, rq.getURL(), rq.getQuantity() };
			data[i++] = tmp;
		}
		return data;
	}

	public static void main(String[] args) {
		new Gui();
	}
}
	
	

